package com.wenzhiguo.myheadline;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.wenzhiguo.myheadline.fragment.CareFragment;
import com.wenzhiguo.myheadline.fragment.HomeFragment;
import com.wenzhiguo.myheadline.fragment.LoginFragment;
import com.wenzhiguo.myheadline.fragment.Menu_Fragment;
import com.wenzhiguo.myheadline.fragment.VoideFragment;

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {

    SlidingMenu menu;

    Fragment fragment;
    HomeFragment homeFragment;
    VoideFragment voideFragment;
    LoginFragment loginFragment;
    CareFragment careFragment;

    private LinearLayout shou;
    private LinearLayout shi;
    private LinearLayout guan;
    private LinearLayout deng;

    private ImageView image1;
    private ImageView image2;
    private ImageView image3;
    private ImageView image4;

    private TextView text1;
    private TextView text2;
    private TextView text3;
    private TextView text4;

    //数组
    int[] bai = new int[]{R.drawable.b_newhome_tabbar, R.drawable.b_newvideo_tabbar, R.drawable.b_newcare_tabbar, R.drawable.b_newnologin_tabbar};
    int[] hei = new int[]{R.drawable.b_newhome_tabbar_press, R.drawable.b_newvideo_tabbar_press, R.drawable.b_newcare_tabbar_press, R.drawable.b_newnologin_tabbar_press};
    ImageView[] image;
    TextView[] text;
    private LinearLayout layout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initMenu();
        //控件
        initView();

    }

    private void initMenu() {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.main_line, Menu_Fragment.newInstance("", ""))
                .commit();

        // configure the SlidingMenu侧滑菜单
        menu = new SlidingMenu(this);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
        menu.setShadowWidthRes(R.dimen.shadow_width);
        menu.setShadowDrawable(R.drawable.shadow);
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        menu.setFadeDegree(0.35f);
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        menu.setMenu(R.layout.cehua_fragment);
    }

    private void support(Fragment f) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (fragment != null) {
            transaction.hide(fragment);
        }
        if (!f.isAdded()) {
            transaction.add(R.id.layout, f);
        }
        transaction.show(f);
        transaction.commit();
        fragment = f;
    }

    private void initView() {
        //父控件
        layout = (LinearLayout) findViewById(R.id.layout);

        shou = (LinearLayout) findViewById(R.id.shouye);
        shi = (LinearLayout) findViewById(R.id.shipin);
        guan = (LinearLayout) findViewById(R.id.guanzhu);
        deng = (LinearLayout) findViewById(R.id.denglu);
        //图片
        image1 = (ImageView) findViewById(R.id.image1);
        image2 = (ImageView) findViewById(R.id.image2);
        image3 = (ImageView) findViewById(R.id.image3);
        image4 = (ImageView) findViewById(R.id.image4);
        //文字
        text1 = (TextView) findViewById(R.id.text1);
        text2 = (TextView) findViewById(R.id.text2);
        text3 = (TextView) findViewById(R.id.text3);
        text4 = (TextView) findViewById(R.id.text4);

        image = new ImageView[]{image1, image2, image3, image4};
        text = new TextView[]{text1, text2, text3, text4};
        //监听
        shou.setOnClickListener(this);
        shi.setOnClickListener(this);
        deng.setOnClickListener(this);
        guan.setOnClickListener(this);
        if (homeFragment == null) {
            homeFragment = new HomeFragment();
        }
        support(homeFragment);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.shouye:
                setBackgroup(0);
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                }
                support(homeFragment);
                break;
            case R.id.shipin:
                setBackgroup(1);
                if (voideFragment == null) {
                    voideFragment = new VoideFragment();
                }
                support(voideFragment);
                break;
            case R.id.guanzhu:
                setBackgroup(2);
                if (careFragment == null) {
                    careFragment = new CareFragment();
                }
                support(careFragment);
                break;
            case R.id.denglu:
                setBackgroup(3);
                if (loginFragment == null) {
                    loginFragment = new LoginFragment();
                }
                support(loginFragment);
                break;
        }
    }

    private void setBackgroup(int a) {
        for (int i = 0; i < bai.length; i++) {
            if (i == a) {
                image[i].setImageResource(hei[i]);
                text[i].setTextColor(Color.RED);
            } else {
                image[i].setImageResource(bai[i]);
                text[i].setTextColor(Color.WHITE);
            }
        }
    }
}
